<template>
  <div class="articles-add">
    <el-dialog
      :title="showTitle"
      :visible="centerDialogVisible"
      width="70%"
      height="800px"
      center

      @close="closeDialogArticle"
    >
      <el-form ref="form" :model="formModel" :rules="rules" label-width="100px">
        <el-form-item label="文章标题:" prop="title">
          <el-input v-model="formModel.title" placeholder="请输入文章标签" />
        </el-form-item>
        <el-form-item label="文章内容:" prop="articleBody">
          <!-- pubForm.content 富文本 -->
          <quill-editor v-model="formModel.articleBody" style="height:300px;margin-bottom:100px" />
        </el-form-item>
        <el-form-item label="视频地址:" prop="videoURL">
          <el-input v-model="formModel.videoURL" placeholder="请输入视频地址" />
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialogArticle">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add, update, detail } from '@/api/hmmm/articles.js'
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
export default {
  name: 'ArticlesAdd',

  components: {
    quillEditor
  },
  props: {
    centerDialogVisible: {
      type: Boolean,
      default: false

    },
    dataRow: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      formModel: {
        id: null,
        title: null,
        articleBody: null,
        videoURL: null
      },
      rules: {
        title: [
          { required: true, message: '请输入文章标题', tirgger: 'blur' }
        ],
        articleBody: [
          { required: true, message: '请输入文章内容', tirgger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formModel.id ? '修改文章' : '新增文章'
    }
  },
  methods: {
    // 关闭弹出框
    closeDialogArticle() {
      this.$emit('update:centerDialogVisible', false)
      // 清空表单
      this.$refs.form.resetFields()
      this.formModel = {
        id: null,
        title: null,
        articleBody: null,
        videoURL: null
      }
    },
    async handleOpen() {
      // console.log(this.dataRow)
      const { data } = await detail(this.dataRow)
      // console.log(res, 111)
      this.formModel = data
    },
    async confirm() {
      this.$refs.form.validate(async valid => {
        if (valid) {
          // 新增
          if (!this.formModel.id) {
            const res = await add(this.formModel)
            console.log(res)
            this.$message.success('添加成功')
          } else {
            const res = await update(this.formModel)
            console.log(res)
            this.$message.success('修改成功')
          }
          this.$emit('updateList')

          this.closeDialogArticle()
        }
      })
    }

  }
}

</script>
